


package com.sakila.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sakila.dbo.RentalDBO;
import com.sakila.util.DBConnection;


public class RentalDAO implements IRentalDAO {





	  private Logger logger = Logger.getLogger(this.getClass());
	//SimpleDateFormat formatter = new SimpleDateFormat(
		//	"yyyy.MM.dd G 'at' HH:mm:ss z");

	/* (non-Javadoc)
	 * @see com.sakila.dao.IRentalDAO#findById(int)
	 */
	@Override
	public RentalDBO findById(int rentalId) {
		RentalDBO dbo = null;
		ResultSet rs = null;
		PreparedStatement statement = null;
		Connection connection = null;
		try {
			connection = DBConnection.getConnection();
			StringBuilder sb = new StringBuilder();
			sb.append("select rental_id as rentalId, rental_date as rentalDate, inventory_id as inventoryId, customer_id as customerId, date as date, staff_id as staffId, last_update as lastUpdate");
			sb.append("from rental ");
			sb.append("where rental_id = " + rentalId);
			statement = connection.prepareStatement(sb.toString());
			logger.info("Executing Query Here in RentalDAO: "+ sb.toString());
			rs = statement.executeQuery();
			if (rs.next()) {
				dbo = new RentalDBO();
				dbo.setRental_id(rs.getInt("rentalId"));
				dbo.setRental_date(rs.getDate("rentalDate"));
				dbo.setInventory_id(rs.getInt("inventoryId"));
				dbo.setCustomer_id(rs.getInt("customerId"));
				dbo.setDate(rs.getDate("date"));
				dbo.setStaff_id(rs.getInt("staffId"));
				dbo.setLast_update(rs.getDate("lastUpdate"));
				
			}

		} catch (SQLException e) {
			logger.error("Exception in RentalDAO" + e );
			throw new RuntimeException(e);
		} finally {
			DBConnection.close(rs, statement, connection);
		}
		return dbo;
	}

	/* (non-Javadoc)
	 * @see com.sakila.dao.IRentalDAO#findAll()
	 */
	@Override
	public List<RentalDBO> findAll() {
		List<RentalDBO> returnList = null;
		ResultSet rs = null;
		PreparedStatement statement = null;
		Connection connection = null;
		RentalDBO dbo = null;
		try {
			connection = DBConnection.getConnection();
			StringBuilder sb = new StringBuilder();
			sb.append("select rental_id as rentalId, rental_date as rentalDate, inventory_id as inventoryId, customer_id as customerId, date as date, staff_id as staffId, last_update as lastUpdate");
			sb.append("from rental ");
			statement = connection.prepareStatement(sb.toString());
			logger.info("Executing Query Here RentalDAO infindAll: "+ sb.toString());
			rs = statement.executeQuery();
			returnList = new ArrayList<RentalDBO>();
			while (rs.next()) {
				dbo = new RentalDBO();
				dbo.setRental_id(rs.getInt("rentalId"));
				dbo.setRental_date(rs.getDate("rentalDate"));
				dbo.setInventory_id(rs.getInt("inventoryId"));
				dbo.setCustomer_id(rs.getInt("customerId"));
				dbo.setDate(rs.getDate("date"));
				dbo.setStaff_id(rs.getInt("staffId"));
				dbo.setLast_update(rs.getDate("lastUpdate"));
				returnList.add(dbo);
				
			}

		} catch (SQLException e) {
			logger.error("Exception in RentalDAO" + e );
			throw new RuntimeException(e);
		} finally {
			DBConnection.close(rs, statement, connection);
		}
logger.debug("Display Me everyone from List:" + returnList);
		return returnList;
	}
	
}

